package com.ms.oschina.base;

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ms.oschina.R;
import com.ms.oschina.util.HandlerUtils;
import com.ms.oschina.util.UiUtils;

/**
 *  界面框架包含加载也，成功页，错误页，和空页面
 * @author Administrator
 * 
 */
public abstract class LoadingPager extends FrameLayout implements
		OnClickListener {

	private ResultCode currentStatus = ResultCode.NONE;
	private View mLoadView;
	private View mEmptyView;
	private View mErrorView;
	private View mSuccessView;
	private boolean onlySuccessView;
	private NetTask task;
	private TextView tv_retry;

	/**
	 * 页面状态枚举类
	 */
	public enum ResultCode {
		STATUS_LOADING, STATUS_EMPTY, STATUS_ERRORS, STATUS_SUCCESS, NONE;
	}

	// 页面显示分析
	// Fragment共性-->页面共性-->视图的展示
	public LoadingPager(Context context) {
		super(context);
		initCommonView();
	}

	/**
	 * 任何应用其实就只有4种页面类型 ① 加载页面 ② 错误页面 ③ 空页面 ④ 成功页面 ①②③三种页面一个应用基本是固定的
	 * 每一个fragment对应的页面④就不一样 进入应用的时候显示①,②③④需要加载数据之后才知道显示哪个
	 */

	/**
	 * 初始化常规视图。
	 */
	private void initCommonView() {
		removeAllViews();

		mLoadView = View.inflate(UiUtils.getContext(), R.layout.pager_loading,
				null);

		mEmptyView = View.inflate(UiUtils.getContext(), R.layout.pager_empty,
				null);
		mEmptyView.findViewById(R.id.empty_img_retry).setOnClickListener(this);
		tv_retry = (TextView) mEmptyView.findViewById(R.id.empty_tv_retry);

		mErrorView = View.inflate(UiUtils.getContext(), R.layout.pager_error,
				null);
		mErrorView.findViewById(R.id.iv_error_retry).setOnClickListener(this);
		mErrorView.findViewById(R.id.error_tv_retry).setOnClickListener(this);

		addView(mLoadView);
		addView(mEmptyView);
		addView(mErrorView);

		// 刷新界面
		refreshInterface();
	}

	/**
	 * 触发加载界面
	 */
	public void initData() {
		if (onlySuccessView) {
			currentStatus = ResultCode.STATUS_SUCCESS;
			refreshInterface();// 刷新界面
			return;
		}
		if (currentStatus == ResultCode.STATUS_SUCCESS) {
			// 数据加载成功，无需再次加载数据
		} else if (currentStatus == ResultCode.STATUS_LOADING) {// 数据正在加载中
		} else {
			currentStatus = ResultCode.STATUS_LOADING;
			refreshInterface();// 刷新界面
			getNetData(false, null);
		}
	}

	/**
	 * 重新加载并获取数据
	 * 
	 * @param onlyloadData
	 */
	public void init() {
		initCommonView();
		currentStatus = ResultCode.STATUS_LOADING;
		refreshInterface();// 刷新界面
		getNetData(false, null);
	}

	public void getNetData(boolean onlyloadData,
			onNetExecuteFinishListener listener) {
		// 联网获取数据
		task = new NetTask();
		task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, onlyloadData,
				listener);// 并行
	}

	@Override
	public void onClick(View v) {
		initData();
	}

	/**
	 * @param code
	 */
	public void setCurStatus(final ResultCode code, final boolean autoRefresh) {
		HandlerUtils.postTaskSafely(new Runnable() {
			@Override
			public void run() {
				if (task != null) {
					task.cancel(true);
				}
				currentStatus = code;
				if (autoRefresh) {
					refreshInterface();
				}
			}
		});
	}

	/**
	 * @param str
	 *            错误页面的显示内容
	 */
	public void setErrorMessage(final String str, final OnClickListener listener) {
		HandlerUtils.postTaskSafely(new Runnable() {
			@Override
			public void run() {
				tv_retry.setText(str);
				tv_retry.setOnClickListener(listener);
			}
		});
	}

	/**
	 * 根据现在的currentStatus 去刷新界面
	 */
	private void refreshInterface() {

		mLoadView.setVisibility(currentStatus == ResultCode.STATUS_LOADING
				|| currentStatus == ResultCode.NONE ? View.VISIBLE : View.GONE);
		mEmptyView
				.setVisibility(currentStatus == ResultCode.STATUS_EMPTY ? View.VISIBLE
						: View.GONE);
		mErrorView
				.setVisibility(currentStatus == ResultCode.STATUS_ERRORS ? View.VISIBLE
						: View.GONE);

		if (currentStatus == ResultCode.STATUS_SUCCESS) {
			mSuccessView = LoadSuccessView();// 加载成功界面

			if (mSuccessView != null) {
				mSuccessView.setVisibility(View.VISIBLE);
				removeView(mSuccessView);
				addView(mSuccessView);
			} else {// 成功视图为null

			}
		}
	}

	private class NetTask extends AsyncTask<Object, Void, ResultCode> {
		private Boolean onlyloadData;
		private onNetExecuteFinishListener listener;

		// 加载数据
		@Override
		protected ResultCode doInBackground(Object... params) {
			this.onlyloadData = (Boolean) params[0];
			this.listener = (onNetExecuteFinishListener) params[1];
			currentStatus = initNetData();// 让子类去实现加载逻辑
			if (currentStatus == null) {// 没有复写
				currentStatus = ResultCode.STATUS_EMPTY;// 设置为空页面
			}
			return currentStatus;
		}

		@Override
		protected void onPostExecute(ResultCode result) {
			// 更新界面
			if (listener != null) {
				listener.onNetExecuteFinish(result);
			}

			if (onlyloadData) {// 仅加载网络
				return;
			}
			refreshInterface();
		}
	}

	public interface onNetExecuteFinishListener {
		void onNetExecuteFinish(ResultCode status);
	}

	public abstract ResultCode initNetData();

	public abstract View LoadSuccessView();

	/**
	 * 如果true 仅显示成功视图
	 * 
	 * @param onlySuccessView
	 */
	public void onlySuccessView(boolean onlySuccessView) {
		this.onlySuccessView = onlySuccessView;
	}

}
